﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Chat.Common;
using System;
using System.Linq;

public class CtlMsgHandle : IHandleable
{
    public void Handle(string cmd)
    {
        if (cmd.Length >= Common.ctlTypeSize)
        {
            string ctlType = cmd.Substring(0, Common.ctlTypeSize).Trim();
            string parameter = cmd.Substring(Common.ctlTypeSize, cmd.Length - Common.ctlTypeSize);

            if (string.Compare(ctlType, ControlType.GetRoomList.ToString(), true) == 0) // 收到房间列表
                GetRoomList(parameter);
            else if (string.Compare(ctlType, ControlType.GetOwner.ToString(), true) == 0)  // 收到自身信息
                GetOwner(parameter);
            else if (string.Compare(ctlType, ControlType.GetMemList.ToString(), true) == 0)  // 收到成员列表
                GetMemList(parameter);
            else if (string.Compare(ctlType, ControlType.EnterServer.ToString(), true) == 0)  // 有成员进入服务器
                EnterServer(parameter);
            else if (string.Compare(ctlType, ControlType.ExitServer.ToString(), true) == 0)  // 有成员退出服务器
                ExitServer(parameter);
            else
                return;
        }
    }
    private void GetRoomList(string jsonStr)
    {
        RoomList roomList = JsonConvert.DeserializeObject<RoomList>(jsonStr);
        if(Singleton<RoomManager>.GetInstance().Count == 0)  // 当前房间列表中没有一个房间
        {
            // 添加所有房间
            foreach (Room room in roomList.Rooms)
                Singleton<RoomManager>.GetInstance().AddOrReplace(room.Id,room);
        }
        else
        {
            List<Room> rcvRooms = new List<Room>(roomList.Rooms);
            List<Room> curRooms = new List<Room>(Singleton<RoomManager>.GetInstance().Dictionary.Values);
            // 添加或修改的房间
            IEnumerable<Room> rooms = rcvRooms.Except(curRooms, new RoomComparer());
            foreach (Room room in rooms)
                Singleton<RoomManager>.GetInstance().AddOrReplace(room.Id, room);

            // 删除的房间
            IEnumerable<Room> rmRooms = curRooms.Except(rcvRooms, new RoomIdComparer());
            foreach (var room in rmRooms)
                Singleton<RoomManager>.GetInstance().Remove(room.Id);

        }
    }
    private void GetMemList(string jsonStr)
    {
        MemList memList = JsonConvert.DeserializeObject<MemList>(jsonStr);
        if (Singleton<MemberManager>.GetInstance().Count == 0)  // 当前成员列表中没有一个成员
        {
            // 添加所有房间
            foreach (Member room in memList.Members)
                Singleton<MemberManager>.GetInstance().AddOrReplace(room.Id, room);
        }
        else
        {
            List<Member> rcvMembers = new List<Member>(memList.Members);
            List<Member> curMembers = new List<Member>(Singleton<MemberManager>.GetInstance().Dictionary.Values);
            // 添加或修改的房间
            IEnumerable<Member> rooms = rcvMembers.Except(curMembers, new MemberComparer());
            foreach (Member room in rooms)
                Singleton<MemberManager>.GetInstance().AddOrReplace(room.Id, room);

            // 删除的房间
            IEnumerable<Member> rmRooms = curMembers.Except(rcvMembers, new MemberIdComparer());
            foreach (var room in rmRooms)
                Singleton<MemberManager>.GetInstance().Remove(room.Id);

        }
    }
    private void GetOwner(string jsonStr)
    {
        Owner owner = JsonConvert.DeserializeObject<Owner>(jsonStr);
        Singleton<Owner>.GetInstance().Id = owner.Id;
        Singleton<Owner>.GetInstance().Name = owner.Name;
    }
    private void EnterServer(string jsonStr)
    {
        Member member = JsonConvert.DeserializeObject<Member>(jsonStr);
        Singleton<MemberManager>.GetInstance().AddOrReplace(member.Id, member);
    }
    private void ExitServer(string jsonStr)
    {
        Member member = JsonConvert.DeserializeObject<Member>(jsonStr);
        Singleton<MemberManager>.GetInstance().Remove(member.Id);
    }
}
